﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace LingDong.NaiveCrawler
{
    public class BaiduCrawl
    {
        public static string GetTextResult(string query)
        {
            string url = String.Format(baiduTextPattern, query);
            url = HtmlParser.HtmlUtility.GetEncodedUrl(url, baiduEncoding);
            return DownloadString(url);
        }

        public static string GetImageResult(string query)
        {
            string url = String.Format(baiduImagePattern, query);
            url = HtmlParser.HtmlUtility.GetEncodedUrl(url, baiduEncoding);
            return DownloadString(url);
        }

        public static int GetTextResultNumber(string query)
        {
            string html = GetTextResult(query);
            Regex reg = new Regex("找到相关结果.*个");
            Match match = reg.Match(html);
            if (match == null || String.IsNullOrEmpty(match.Value))
                return 0;

            string value = match.Value.Substring(6, match.Value.Length - 7);
            if (value.StartsWith("约"))
                value = value.Substring(1);
            value = value.Replace(",", "");

            int result;
            if (Int32.TryParse(value, out result))
                return result;
            return 0;
        }

        public static int GetImageResultNumber(string query)
        {
            string html = GetImageResult(query);
            Regex reg = new Regex("找到相关图片.*张");
            Match match = reg.Match(html);
            if (match == null || String.IsNullOrEmpty(match.Value))
                return 0;

            string value = match.Value.Substring(6, match.Value.Length - 7);
            if (value.StartsWith("约"))
                value = value.Substring(1);
            value = value.Replace(",", "");

            int result;
            if (Int32.TryParse(value, out result))
                return result;
            return 0;
        }

        private static string DownloadString(string url)
        {
            string html = String.Empty;
            try
            {
                using (WebClient client = new WebClient())
                {
                    client.Headers[HttpRequestHeader.AcceptLanguage] = "zh-CN";
                    client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; Maxthon; GTB6.4; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.5.21022; .NET CLR 3.5.30729; InfoPath.2; .NET CLR 3.0.30729)";
                    client.Credentials = CredentialCache.DefaultCredentials;
                    html = client.DownloadString(url);
                }
            }
            catch (Exception)
            {
                return String.Empty;
            }
            return html;
        }

        private static Encoding baiduEncoding = Encoding.GetEncoding("GB2312");
        private static string baiduTextPattern = @"http://www.baidu.com/s?wd={0}";
        private static string baiduImagePattern = @"http://image.baidu.com/i?tn=baiduimage&ct=201326592&lm=-1&cl=2&word={0}";
    }
}
